package com.yang.controller;

import com.yang.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.InternalResourceView;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/view")
public class viewHandler {


    @RequestMapping("/map")
    public String map(Map<String, Object> map) {
        User user = new User();
        user.setId(1);
        user.setName("张三");
        map.put("user", user);
        return "show";
    }

    @RequestMapping("/model")
    public String model(Model model) {
        User user = new User();
        user.setId(1);
        user.setName("张三");
        model.addAttribute("user", user);
        return "show";
    }

    @RequestMapping("/modeAndView1")
    public ModelAndView modeAndView1() {
        ModelAndView modelAndView = new ModelAndView();
        User user = new User();
        user.setId(1);
        user.setName("张三");
        //填充业务数据
        modelAndView.addObject("user", user);
        //绑定视图
        modelAndView.setViewName("show");
        return modelAndView;
    }

    @RequestMapping("/mav2")
    public ModelAndView modelAndView2() {
        ModelAndView modelAndView = new ModelAndView();
        User user = new User();
        user.setId(1);
        user.setName("张三");
        modelAndView.addObject("user", user);
        View view = new InternalResourceView("/show.jsp");
        modelAndView.setView(view);
        return modelAndView;
    }

    @RequestMapping("/mav3")
    public ModelAndView modelAndView3() {
        ModelAndView modelAndView = new ModelAndView("show");
        User user = new User();
        user.setId(1);
        user.setName("张三");
        modelAndView.addObject("user", user);
        return modelAndView;
    }

    @RequestMapping("/mav4")
    public ModelAndView modelAndView4() {
        View view = new InternalResourceView("/show.jsp");
        ModelAndView modelAndView = new ModelAndView(view);
        User user = new User();
        user.setId(1);
        user.setName("张三");
        modelAndView.addObject("user", user);
        return modelAndView;
    }

    @RequestMapping("/mav5")
    public ModelAndView modelAndView5() {
        Map<String, Object> map = new HashMap<>();
        User user = new User();
        user.setId(1);
        user.setName("张三");
        map.put("user", user);
        ModelAndView modelAndView = new ModelAndView("show", map);
        return modelAndView;
    }

    @RequestMapping("/mav6")
    public ModelAndView modelAndView6() {
        Map<String, Object> map = new HashMap<>();
        User user = new User();
        user.setId(1);
        user.setName("张三");
        map.put("user", user);
        View view = new InternalResourceView("/show.jsp");
        ModelAndView modelAndView = new ModelAndView(view, map);
        return modelAndView;
    }

    @RequestMapping("/mav7")
    public ModelAndView modelAndView7() {
        User user = new User();
        user.setId(1);
        user.setName("张三");
        ModelAndView modelAndView = new ModelAndView("show", "user", user);
        return modelAndView;
    }

    @RequestMapping("/mav8")
    public ModelAndView modelAndView8() {
        User user = new User();
        user.setId(1);
        user.setName("张三");
        View view = new InternalResourceView("/show.jsp");
        ModelAndView modelAndView = new ModelAndView(view, "user", user);
        return modelAndView;
    }
}
